<template>
  <div class="search-container">
    <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
      <el-form-item label="患者姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          placeholder="请输入患者姓名"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="床号" prop="bedNo">
        <el-input
          v-model="queryParams.bedNo"
          clearable
          placeholder="请输入床号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
      </el-form-item>
    </el-form>
  </div>
  <el-table v-adaptive="390" :border="true" :data="patientBedList"
            :header-cell-style="$headerCellStyle" :stripe="true"
            @selection-change="handleSelectionChange">
    <el-table-column v-if="isMultiple" :align="'center'" type="selection" width="55"/>
    <el-table-column v-if="!isMultiple" :align="'center'" fixed="left" width="45">
      <template #default="scope">
        <el-radio-group v-model="currentRow" @change="radioClick">
          <el-radio :value="scope.row.visitId"/>
        </el-radio-group>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="床号" prop="bedNo" width="70">
      <template #default="scope">
        <span class="ys-css">{{ scope.row.bedNo }}</span>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="房号" prop="roomNo" width="70"/>
    <el-table-column :align="'center'" fixed="left" label="患者信息" prop="name"
                     width="130">
      <template #default="scope">
        <span class="ys-css">{{ scope.row.name }}</span>
        <span style="margin-left: -10px">
                            <svg-icon :icon-class="dictStore.getMateDictInfo(
                                dictDataMap['sys_user_sex'],
                                scope.row.sex
                            ).dictValue === '1'
                                ? 'man'
                                : 'woman'
                                " style="margin-left: 10px"/>
                        </span>
        {{ scope.row.ageAll }}
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="住院号" prop="hospitalNo" width="140">
      <template #default="scope">
        <span class="ys-css">{{ scope.row.hospitalNo }}</span>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="身份证号" prop="idNo" width="160"/>
  </el-table>
</template>

<script lang="ts" setup>
//只查询 执行状态中的药品医嘱（临时：状态临时执行；长期：状态校对）的患者信息

import {getExistExecuteDrugList} from "@/api/orders/orders.ts"
import {useUserStoreHook} from "@/store/modules/user.ts";
import {useDictStoreHook} from "@/store/modules/dict.ts";

const useUserStore = useUserStoreHook();

const dictStore = useDictStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;

const emits = defineEmits(["patientSelectionChange"])
let dictDataMap = ref(dictStore.isTypeGetData('medical_insurance_type', 'sys_user_sex', 'disease_dict'
))
const {proxy} = getCurrentInstance() as any;
const currentSelectDept = proxy.$currentSelectDept
const patientBedList = ref<any>()
const currentRow = ref<any>("")
const visitIds = ref<any>([])
const props = defineProps({
  isMultiple: {
    type: Boolean,
    default: true
  }
})
const queryParams = ref<any>({
  orgId: userInfo.orgId,
  wardCode: currentSelectDept,
  bedNo: ''
})

function getList() {
  getExistExecuteDrugList(queryParams.value).then(res => {
    patientBedList.value = res.data
  })
}

/*
表格单选
 */
function radioClick(value: string) {
  queryParams.value.visitId = value
  emits("patientSelectionChange", queryParams.value.visitId);
}

function handleQuery() {
  getList()
}

function resetQuery() {
  queryParams.value.name = ""
  queryParams.value.hospitalNo = ""
  getList()
}

function handleSelectionChange(selection) {
  visitIds.value = selection.map((item) => item.visitId);
  emits("patientSelectionChange", visitIds.value);
}

defineExpose({
  handleQuery
})
</script>
<style scoped>

:deep(.el-radio.el-radio--large) {
  height: 0;
}

</style>
